package cn.exrick.xboot.common.utils.jiuqi.utils;

import cn.exrick.xboot.common.utils.SpringUtils;
import cn.exrick.xboot.modules.getConfig.entity.JiuQiConfig;
import lombok.extern.slf4j.Slf4j;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

@Slf4j
public class ServeUtil {

    private static JiuQiConfig JiuQiConfigS  = (JiuQiConfig) SpringUtils.getBean("jiuQiConfig");

    //获取配置文件中后台访问的IP地址
    public static String getInet4Address(){
        String portalIp = JiuQiConfigS.getPortalIp();
        log.info("当前后台IP："+portalIp);
        return portalIp;
    }

    public static String getFilrProject(){
        String FilrProjectPort = JiuQiConfigS.getFilrProject();
        log.info("获取配置文件服务器的IP地址："+FilrProjectPort);
        return FilrProjectPort;
    }




  /*  public static String getInet4Address() {
        Enumeration<NetworkInterface> nis;
        String ip = null;
        try {
            nis = NetworkInterface.getNetworkInterfaces();
            for (; nis.hasMoreElements();) {
                NetworkInterface ni = nis.nextElement();
                Enumeration<InetAddress> ias = ni.getInetAddresses();
                for (; ias.hasMoreElements();) {
                    InetAddress ia = ias.nextElement();
                    //ia instanceof Inet6Address && !ia.equals("")
                    if (ia instanceof Inet4Address && !ia.getHostAddress().equals("127.0.0.1")) {
                        ip = ia.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ip;
    }
*/














    public static String INTRANET_IP = getIntranetIp(); // 内网IP
    //public static String INTERNET_IP = getInet4Address(); // 外网IP
    /**
     * 获得内网IP
     * @return 内网IP
     */
    private static String getIntranetIp(){
        try{
            return InetAddress.getLocalHost().getHostAddress();
        } catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 获得外网IP
     * @return 外网IP
     */
    public static String getINTERNET(){

        try{
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            Enumeration<InetAddress> addrs;
            while (networks.hasMoreElements())
            {
                addrs = networks.nextElement().getInetAddresses();
                while (addrs.hasMoreElements())
                {
                    ip = addrs.nextElement();
                    if (ip != null
                            && ip instanceof Inet4Address
                            && ip.isSiteLocalAddress()
                            && !ip.getHostAddress().equals(INTRANET_IP))
                    {
                        return ip.getHostAddress();
                    }
                }
            }

            // 如果没有外网IP，就返回内网IP
            return INTRANET_IP;
        } catch(Exception e){
            throw new RuntimeException(e);
        }
    }




    public static void main(String[] args) {
        try{
            System.out.println(ServeUtil.getInet4Address());

            InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址

            System.out.println(address.getHostAddress());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
